//
//  bigImageViewController.m
//  INAlienLand
//
//  Created by dllo on 15/7/10.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "bigImageViewController.h"
#import "HTTPTool.h"
#import "thanksCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "AllImage.h"

@interface bigImageViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,retain)UICollectionView *cityCollect;
@property (nonatomic,retain)NSMutableArray *cityArr;
@property (nonatomic,assign)NSInteger m;

@end

@implementation bigImageViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.m = 0;
    }
    return self;
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.cityArr = [NSMutableArray array];
    [self createData];
    [self createcollect];
    [self.cityCollect addFooterWithCallback:^{
        [self createData];
        [self.cityCollect footerEndRefreshing];
    }];
}
-(void)createcollect
{
    
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
    flowlayout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    flowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowlayout.minimumLineSpacing = 0;
    self.cityCollect = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64-49) collectionViewLayout:flowlayout];
    [self.view addSubview:self.cityCollect];
    self.cityCollect.backgroundColor = [UIColor colorWithRed:0.960 green:0.968 blue:1.000 alpha:1.000];
    self.cityCollect.pagingEnabled =YES;
    self.cityCollect.delegate = self;
    self.cityCollect.dataSource = self;
    [self.cityCollect registerClass:[thanksCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.cityArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    thanksCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    NSString *str = self.cityArr[indexPath.row];

    if (indexPath.row ==(self.m +1)*18-1) {
        [self createData];
        self.m++;
    }
    [cell.cityImage sd_setImageWithURL:[NSURL URLWithString:str]];
    
    
    
    
    return cell;

}

-(void)createData
{
    static NSInteger i = 0;
    i=i+18;
    [HTTPTool get:[NSString stringWithFormat:@"http://api.breadtrip.com//destination/place/%@/%@/photos/?gallery_mode=1&count=%ld&",self.arer.type,self.arer.bId,i] body:nil HTTPResult:JSON success:^(id result) {
        NSDictionary *dic = result;
        NSArray *Arr =dic[@"items"];
        for (NSDictionary *tempDic in Arr) {
            NSString *str = tempDic[@"photo"];
            [self.cityArr addObject:str];
            
            
        }
        
        [self.cityCollect reloadData];
        NSLog(@"111");
    } failure:^(NSError *error) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
